package it.getta.vicinometro.utils;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class MapItemizedOverlay extends ItemizedOverlay<OverlayItem> {

	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	private OnTapListener mOnTapListener;
	
	public interface OnTapListener {
		public void onTap(GeoPoint point);
	}

	private MapItemizedOverlay(Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
	}

	public MapItemizedOverlay(Drawable defaultMarker, Context context, OnTapListener tapListener) {
		this(defaultMarker);
		mOnTapListener = tapListener;
		
	} 

	@Override
	public boolean onTap(int index) {
		OverlayItem item = mOverlays.get(index);
//		AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
//		builder.setTitle(item.getTitle());
//		builder.setMessage(item.getSnippet());
//		
//		AlertDialog dialog = builder.create();
//		dialog.show();
		
		if(mOnTapListener != null) {
			mOnTapListener.onTap(item.getPoint());
		}
		
		return false;
	}
	
	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

	@Override
	public int size() {
		return mOverlays.size();
	}

	public void addOverlay(OverlayItem overlay, GeoPoint pointToLink, MapView map) {
//		Paint paint = new Paint();
//		paint.setColor(Color.RED);
//		
//		Projection proj = map.getProjection();
//		Point start = proj.toPixels(overlay.getPoint(), null);
//		Point stop = proj.toPixels(pointToLink, null);
//		
//		Canvas c = new Canvas();
//	
//		c.drawLine(start.x, start.y, stop.x, stop.y, paint);
//		
//		draw(c, map, false);
		mOverlays.add(overlay);
		populate();
	}

	public OnTapListener getOnTapListener() {
		return mOnTapListener;
	}

	public void setOnTapListener(OnTapListener mOnTapListener) {
		this.mOnTapListener = mOnTapListener;
	}

}
